package com.ctu.gzero.container
{
	import com.ctu.gzero.edit.ISelectionManager;
	import com.ctu.gzero.edit.SelectionFormat;
	import com.ctu.gzero.utils.TypeCursor;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	
	public class BaseController
	{
		protected var _container:DisplayObjectContainer;
		protected var _interactionManager:ISelectionManager;
		private var blinkTimer:Timer;
		private var blinkCursor:TypeCursor;
		private var backgroundSprite:Shape;
		private var selectionSprite:Shape = null;
		
		public function BaseController(container:DisplayObjectContainer)
		{
			_container = container;
		}
		
		protected function attachBackgroundShape(width:Number, height:Number):void
		{
			backgroundSprite = new Shape();
			backgroundSprite.graphics.clear();
			backgroundSprite.graphics.beginFill(0xFFFFFF, 0);
			backgroundSprite.graphics.drawRect(0, 0, width, height);
			_container.addChild(backgroundSprite);
		}
		
		protected function drawSelection(format:SelectionFormat, x:Number,y:Number, width:Number,height:Number):void
		{
			if(selectionSprite==null)
			{
				selectionSprite = new Shape();
			}			
		  	selectionSprite.graphics.beginFill(format.color);
		  	selectionSprite.graphics.drawRect(x,y, width,height)
		  	selectionSprite.alpha = format.alpha;
		  	selectionSprite.graphics.endFill();		  	
//		  	if(interactionManager.blinking)
//		  	{
//		  		startBlinkingCursor(__s);
//		  	}
		  	_container.addChild(selectionSprite);
		}
		
		protected function hideSelection():void
		{
			if(selectionSprite!=null) {
				selectionSprite.graphics.clear();
			}
		}
		
		protected function attachAllListener():void
		{
			_container.addEventListener(Event.ACTIVATE, activateHandler);
			_container.addEventListener(Event.DEACTIVATE, deactivateHandler);
			_container.addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			_container.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);
			_container.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			_container.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			_container.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_container.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			_container.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			_container.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			_container.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			_container.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			_container.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
		}
		
		protected function detachAllListerner():void
		{
			_container.removeEventListener(Event.ACTIVATE, activateHandler);
			_container.removeEventListener(Event.DEACTIVATE, deactivateHandler);
			_container.removeEventListener(FocusEvent.FOCUS_IN, focusInHandler);
			_container.removeEventListener(TextEvent.TEXT_INPUT, textInputHandler);
			_container.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			_container.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			_container.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_container.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			_container.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			_container.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			_container.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			_container.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			_container.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
		}
		
		protected function activateHandler(event:Event):void
		{
			//trace("activateHandler");
			_container.focusRect = false;
			_container.stage.focus = _container;
		}
		
		protected function deactivateHandler(event:Event):void
		{
		}
		
		protected function focusInHandler(event:FocusEvent):void{
			//trace("focusInHandler");
			if(_interactionManager)
				_interactionManager.focusInHandler(event);
		}
		
		protected function textInputHandler(event:TextEvent):void
		{
			//trace("keyDownHandler");
			if(_interactionManager)
				_interactionManager.textInputHandler(event);
		}
		
		protected function keyDownHandler(event:KeyboardEvent):void
		{
			//trace("keyDownHandler");
			if(_interactionManager)
				_interactionManager.keyDownHandler(event);		
		}
		
		protected function keyUpHandler(event:KeyboardEvent):void
		{
			if(_interactionManager)
				_interactionManager.keyUpHandler(event);
		}
		
		protected function mouseDownHandler(event:MouseEvent):void
		{
			trace("mouseDownHandler");
			_container.focusRect = false;
			_container.stage.focus = _container;
			
			if(_interactionManager)
				_interactionManager.mouseDownHandler(event);	
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.mouseUpHandler(event);	
		}
		
		protected function mouseMoveHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.mouseMoveHandler(event);	
		}
		
		protected function mouseOverHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.mouseOverHandler(event);	
		}
		
		protected function mouseOutHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.mouseOutHandler(event);	
		}
		
		protected function rollOutHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.rollOutHandler(event);	
		}
		
		protected function rollOverHandler(event:MouseEvent):void
		{
			if(_interactionManager)
				_interactionManager.rollOverHandler(event);	
		}
		
		private function startBlinkCursor():void
		{
			if(blinkTimer == null)
			{
				blinkTimer = new Timer(500);
				blinkTimer.addEventListener(TimerEvent.TIMER, blinkTimerHandler);
			}
			blinkTimer.stop();
			blinkTimer.start();
		}
		
		private function stopBlinkCursor():void
		{
			if(blinkTimer)
				blinkTimer.stop();
		}
		
		private function blinkTimerHandler(event:TimerEvent):void
		{
			if(blinkCursor)
			{
				blinkCursor.alpha = (blinkCursor.alpha == 0)? 1: 0; 
			}
		}
		
		protected function drawBlinkCursor(x:Number, y:Number, width:Number, height:Number):void
		{
			if(blinkCursor == null)
			{
				blinkCursor = new TypeCursor(x,y,width,height);
				_container.addChild(blinkCursor);
				blinkCursor.startBlinkCursor();
			}
			else 
			{
				blinkCursor.stopBlinkCursor();
				blinkCursor.updateCursor(x,y,width,height);
				blinkCursor.startBlinkCursor();
			}
		}
	}
}